package demo.java.java8.Lambdas.demo7;

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class MySpliterator {

	public int countWordsIteratively(String s){
		int count=0;
		boolean lastSpace=true;
		for(char c : s.toCharArray()){
			if(Character.isWhitespace(c)){
				lastSpace=true;
			}else{
				if(lastSpace){
					count++;
				}
				lastSpace=false;
			}
		}
		return count;
	}
	
	public int countWords(Stream<Character> stream){
		WordCounter wordCounter=stream.reduce(
				new WordCounter(0, true),WordCounter::accumulate,WordCounter::combine
				);
		return wordCounter.getCounter();
	}
	
	public static void main(String[] args) {
		String sentence=
				"Nel mezzo del cammin di nostra vita " +
				"mi ritrovai in una selva oscura" +
				" ché la dritta via era smarrita ";
		MySpliterator mySpliterator=new MySpliterator();
//		int count = mySpliterator.countWordsIteratively(sentence);
//		System.out.println(count);
		
		Stream<Character> stream = IntStream.range(0, sentence.length())
				.mapToObj(sentence::charAt);
		System.out.println(mySpliterator.countWords(stream));
//		System.out.println(stream.count());
		
		
	}
}
